package com.redoor.app.stake.configurer;

import com.redoornetwork.framework.datasource.util.SpringContextUtil;
import com.redoornetwork.framework.mybatis.configurer.AbstractMybatisConfigurer;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * mybatis
 * Created by Floki on 2018/1/16.
 */
@Configuration
public class MybatisConfigurer extends AbstractMybatisConfigurer {
    /**
     * 项目基础包名称，根据公司实际项目修改
     */
    public static final String[] BASE_PACKAGES = new String[]{"com.redoor.app.stake.auth","com.redoor.app.stake.basicdata","com.redoor.app.stake.workflow"};

    @Override
    public String getTypeAliasesPackage() {
        StringBuffer BASE_PACKAGE = new StringBuffer("");
        for (String pageage : BASE_PACKAGES) {
            BASE_PACKAGE.append(pageage).append(".model,") ;
        }
        if(BASE_PACKAGE.length() > 1){
            BASE_PACKAGE.delete(BASE_PACKAGE.length()-1,BASE_PACKAGE.length());
        }
        return BASE_PACKAGE.toString();
    }

    @Override
    public String getBasePackage() {
        StringBuffer BASE_PACKAGE = new StringBuffer("");
        for (String pageage : BASE_PACKAGES) {
            BASE_PACKAGE.append(pageage).append(".dao,") ;
        }
        if(BASE_PACKAGE.length() > 1){
            BASE_PACKAGE.delete(BASE_PACKAGE.length()-1,BASE_PACKAGE.length());
        }
        return BASE_PACKAGE.toString();
    }


    @Override
    public String getMapperLocations() {
        return "classpath*:com/redoor/app/stake/**/mapper/*.xml";
    }

    @Bean
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
        DataSource dataSource = (DataSource) SpringContextUtil.getBean("roundRobinDataSourceProxy");
        return getSqlSessionFactory(dataSource);
    }

    //事务管理
    @Bean
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new DataSourceTransactionManager((DataSource) SpringContextUtil.getBean("roundRobinDataSourceProxy"));
    }

}
